#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    model = new QFileSystemModel(this);
    model->setRootPath(QDir::currentPath());

    ui->treeView->setModel(model);
    ui->tableView->setModel(model);
    ui->listView->setModel(model);

    connect(ui->treeView, &QTreeView::clicked, ui->listView, &QListView::setRootIndex);
    connect(ui->treeView, &QTreeView::clicked, ui->tableView, &QTableView::setRootIndex);
    connect(ui->treeView, &QTreeView::clicked, this, &MainWindow::on_treeview_clicked);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_treeview_clicked(const QModelIndex &index)
{
    ui->lFileName->setText(model->fileName(index));
    ui->lFileType->setText(model->type(index));
    ui->lFilePath->setText(model->filePath(index));
    ui->cboxIsDir->setChecked(model->isDir(index));

    int sz = model->size(index) / 1024;
    if (sz <1024) {
        ui->lFileSize->setText(QString("%1 KB").arg(sz));
    }
    else {
        ui->lFileSize->setText(QString::asprintf("%.1f MB", sz / 1024.0));
    }
}

